﻿
using PEProtocol;

public class GuideSys{

    private static GuideSys instance = null;

    public static GuideSys Instance{
        get{
            if (instance == null){
                instance = new GuideSys();
            }
            return instance;
        }
    }

    private CacheSvc cacheSvc;
    private CfgSvc cfgSvc = null;
    
    public void Init(){
        cacheSvc = CacheSvc.Instance;
        cfgSvc = CfgSvc.Instance;
        PECommon.Log("GuideSys Init Done");
    }

    public void ReqGuide(MsgPack pkt){
        ReqGuide data = pkt.msg.reqGuide;

        GameMsg msg = new GameMsg{
            cmd = (int) CMD.ResGuide
        };

        PlayerData pd = cacheSvc.GetPlayerDataBySession(pkt.session);
        GuideCfg gc = cfgSvc.GetGuideData(data.guideid);

        //更新引导ID
        if (pd.guideid == data.guideid){
            //检测是否为智者点播任务
            if (pd.guideid == 1001){
                TaskSys.Instance.CalcTaskPrgs(pd, 1);
            }
            
            pd.guideid += 1;
            //更新玩家数据
            pd.coin += gc.coin;
            PECommon.CalcExp(pd, gc.exp);

            if (!cacheSvc.UpdatePlayerData(pd.id, pd)){
                msg.err = (int) ErrorCode.UpdateDBError;
            }
            else{
                msg.resGuide = new ResGuide{
                    guideid = pd.guideid,
                    coin = pd.coin,
                    lv = pd.level,
                    exp = pd.exp
                };
            }
        }
        else{
            msg.err = (int)ErrorCode.ServerDataError;
        }

        pkt.session.SendMsg(msg);
    }
}
